package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Satisfaction;
import com.ruoyi.system.domain.bo.SatisfactionBo;
import com.ruoyi.system.domain.vo.SatisfactionVo;
import com.ruoyi.system.mapper.SatisfactionMapper;
import com.ruoyi.system.service.ISatisfactionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 勤务满意度统计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-16
 */
@RequiredArgsConstructor
@Service
public class SatisfactionServiceImpl implements ISatisfactionService {

    private final SatisfactionMapper baseMapper;

    /**
     * 查询勤务满意度统计
     */
    @Override
    public SatisfactionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询勤务满意度统计列表
     */
    @Override
    public TableDataInfo<SatisfactionVo> queryPageList(SatisfactionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Satisfaction> lqw = buildQueryWrapper(bo);
        Page<SatisfactionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询勤务满意度统计列表
     */
    @Override
    public List<SatisfactionVo> queryList(SatisfactionBo bo) {
        LambdaQueryWrapper<Satisfaction> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Satisfaction> buildQueryWrapper(SatisfactionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Satisfaction> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOfficerId() != null, Satisfaction::getOfficerId, bo.getOfficerId());
        lqw.eq(bo.getDutyDate() != null, Satisfaction::getDutyDate, bo.getDutyDate());
        lqw.eq(bo.getSatisfactionScore() != null, Satisfaction::getSatisfactionScore, bo.getSatisfactionScore());
        lqw.eq(bo.getResponseTime() != null, Satisfaction::getResponseTime, bo.getResponseTime());
        lqw.eq(bo.getCrowdSatisfaction() != null, Satisfaction::getCrowdSatisfaction, bo.getCrowdSatisfaction());
        lqw.eq(bo.getEventHandleRate() != null, Satisfaction::getEventHandleRate, bo.getEventHandleRate());
        lqw.eq(bo.getPatrolCoverage() != null, Satisfaction::getPatrolCoverage, bo.getPatrolCoverage());
        lqw.eq(bo.getEquipmentStatusRate() != null, Satisfaction::getEquipmentStatusRate, bo.getEquipmentStatusRate());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), Satisfaction::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增勤务满意度统计
     */
    @Override
    public Boolean insertByBo(SatisfactionBo bo) {
        Satisfaction add = BeanUtil.toBean(bo, Satisfaction.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改勤务满意度统计
     */
    @Override
    public Boolean updateByBo(SatisfactionBo bo) {
        Satisfaction update = BeanUtil.toBean(bo, Satisfaction.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Satisfaction entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除勤务满意度统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
